import http from '../../utils/http'
import type { ApiResponse, PageResult } from '../../utils/ApiResponse'
import {Form, PageUser, Role, User} from "./types/user.dto";


// 退出
export const userLogout = (): Promise<ApiResponse<Object>> => {
    return http.get('/sysUser/logout')
}

// 获取所有用户数据
export const getAllUsers = (data: PageUser): Promise<PageResult<Object>> => {
    return http.get(`/sysUser/info`, {
        params: {
            page: data.page,
            limit: data.limit,
            username: data.username,
            role: data.role,
            startCreateTime: data.startCreateTime,
            endCreateTime: data.endCreateTime
        }
    })
}

// 获取角色列表
export const getRoleList = (): Promise<ApiResponse<Role[]>> => {
    return http.get('/sysRole/info')
}

// 根据id获取用户信息
export const getUserInfoById = (id: string): Promise<ApiResponse<User>> => {
    return http.get(`/sysUser/info/${id}`)
}

// 删除用户
export const deleteUserById = (id: string): Promise<ApiResponse<Object>> => {
    return http.delete(`/sysUser/delete/${id}`)
}

// 修改用户信息
export const saveUserInfo = (data: User): Promise<ApiResponse<Object>> => {
    return http.post('/sysUser/save', data)
}

// 新增用户
export const addUser = (data: User): Promise<ApiResponse<Object>> => {
    return http.post('/sysUser/save', data)
}

// 获取用户信息
export const getProfile = (id: number) => {
    return http.get('/sysUser/profile/' + id )
};

// 修改密码
export const updatePassword = (data: { oldPassword: string; newPassword: string }) => {
    return http({
        url: "/sysUser/updatePassword",
        method: "put",
        data,
    });
};

// 修改个人信息
export const updateUserInfo = (data: Form) => {
    return http({
        url: "/sysUser/save",
        method: "post",
        data,
    });
};

// 注册用户
export const register = (data: Form) => {
    return http({
        url: "/sysUser/register",
        method: "post",
        data,
    });
};

